package com.usian.controller;

import com.sun.org.apache.xpath.internal.operations.Mod;
import com.usian.pojo.User;
import com.usian.service.UserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.jws.WebParam;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/user")
public class UserController {

    private final static Logger logger = LoggerFactory.getLogger(UserController.class);

    @Autowired
    private UserService userService;
    private Integer userId;


    @RequestMapping("/index/{id}/{name}/{age}")
    public String index(Model model ,@PathVariable int id, @PathVariable  String name,@PathVariable int age){

        int i=20/0;
        model.addAttribute("name","哈哈你好");
        model.addAttribute("address","北京市海淀区北京大学西门");
        Date date = new Date();
        model.addAttribute("date",date);
        System.out.println(id +"=="+name+"==="+age);
        return "index";
    }

    /**
     * 手动书写 查询所有
     * @return
     */
    @RequestMapping("/findAll")
    public String findAll(Model model){
        List<User> userList = userService.findAll();
        model.addAttribute("userList",userList);
        System.out.println("=================this is a findAll methods");
        logger.info("logger 答应的 info级别日志");
        logger.debug("logger 答应的 debug级别日志");
        logger.error("logger 答应的 error级别日志");
        return "list";
    }

    /**
     * 通用tkmapper
     * @return
     */
    @RequestMapping("/findById/{id}")
    @ResponseBody
    public User findById(@PathVariable Integer id){
        User user = userService.findById(id);
        return user;
    }

    /**
     * 进入新增页面
     * @return
     */
    @RequestMapping("/toAdd")
    public String toAdd(){
        return "add";
    }

    /**
     * 保存
     * @param user
     * @return
     */
    @RequestMapping("/addUser")
    public String addUser(User user){
        if(user.getUserId()!=null){
            userService.updateUser(user);
        }else{
            userService.addUser(user);
        }
        return "redirect:/user/findAll";
    }

    /**
     * 进入修改页面
     *
     * @param model
     * @param userId
     * @return
     */
    @RequestMapping("/toUpdate")
    public String toUpdate(Model model,Integer userId){
        User user = userService.findById(userId);
        model.addAttribute("user",user);
        return "update";
    }

    /**
     * 根据id删除
     *
     * @param userId
     * @return
     */
    @RequestMapping("/toDelete")
    public String toDelete(Integer userId){
        this.userId = userId;
        userService.deleteById(userId);
        return "redirect:/user/findAll";
    }
}
